#include <vector>

#include "definition.h"

using namespace std;
// 没想到使用depth变量来看ans是否添加完毕

class Solution {
 public:
  vector<int> ans;
  int depth = 0;
  vector<int> rightSideView(TreeNode* root) {
    func(root);
    return ans;
  }
  void func(TreeNode* root) {
    if (!root) return;
    // ans.push_back(root->val);
    ++depth;
    if (ans.size() < depth) ans.push_back(root->val);
    func(root->right);
    func(root->left);
    --depth;
  }
};

/*
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        vector<int> result;
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (i == (size - 1)) result.push_back(node->val); //
将每一层的最后元素放入result数组中 if (node->left) que.push(node->left); if
(node->right) que.push(node->right);
            }
        }
        return result;
    }
};
*/